package cn.toleo.sso.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import cn.toleo.common.pojo.ToleoResult;
import cn.toleo.jedis.JedisClient;
import cn.toleo.mapper.TbUserMapper;
import cn.toleo.pojo.TbUser;
import cn.toleo.pojo.TbUserExample;
import cn.toleo.pojo.TbUserExample.Criteria;
import cn.toleo.sso.service.UserService;
import cn.toleo.utils.JsonUtils;

/**
 * 用户处理service
 * @author Administrator
 *
 */

@Service
public class UserServiceImpl implements UserService{
	@Autowired
	private TbUserMapper userMapper;

	@Override
	public ToleoResult checkData(String data, int type) {
		
		//主干基础,可选参数1、2、3分别代表username、phone、email
		TbUserExample example = new TbUserExample();
		Criteria criteria = example.createCriteria();
		if(type==1){
			criteria.andUsernameEqualTo(data);
		}else if(type==2){
			criteria.andPhoneEqualTo(data);
		}else if(type==3){
			criteria.andEmailEqualTo(data);
		}else{
			return ToleoResult.build(100, "参数中包含非法数据");
		}
		//主干
		List<TbUser> list = userMapper.selectByExample(example);
		if( list !=null && list.size()>0){
			return ToleoResult.ok(false);
		}
		
		//结果
		return ToleoResult.ok(true);
	}
//------------------------------------------------------------------------------------------------
	@Override
	public ToleoResult regidter(TbUser user) {
		//主干基础
		if(StringUtils.isBlank(user.getUsername())){
			return ToleoResult.build(400, "用户名不能为空");
		}
		
		ToleoResult ToleoResult = checkData(user.getUsername(),1);
		if(!(boolean)ToleoResult.getData()){
			return ToleoResult.build(400, "用户名重复");
		}
		
		if(StringUtils.isBlank(user.getPassword())){
			return ToleoResult.build(400, "密码为空");
		}
		
		if(StringUtils.isNotBlank(user.getPhone())){
			ToleoResult checkData = checkData(user.getPhone(), 2);
			if(!(boolean)checkData.getData()){
				return ToleoResult.build(400, "电话已经存在");
			}
		}
		
		if(StringUtils.isNotBlank(user.getEmail())){
			ToleoResult checkData = checkData(user.getEmail(), 3);
			if(!(boolean)checkData.getData()){
				return ToleoResult.build(400, "email已经存在");
			}
		}
		//主干
		user.setCreated(new Date());
		user.setUpdated(new Date());
		String md5DigestAsHex = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
		user.setPassword(md5DigestAsHex);
		userMapper.insert(user);
		
		//结果
		return ToleoResult.ok();
	}

	//------------------------------------------------------------------------------------------------
	@Autowired
	private JedisClient jedisClient;
	@Value("${USER_SESSION}")
	private String USER_SESSION;
	@Value("${SESSION_EXPIRE}")
	private Integer SESSION_EXPIRE;
	
	@Override
	public ToleoResult login(String username, String password) {
		//主干基础
		TbUserExample tbUserExample = new TbUserExample();
		Criteria criteria = tbUserExample.createCriteria();
		criteria.andUsernameEqualTo(username);
		List<TbUser> list = userMapper.selectByExample(tbUserExample);
		if(list==null&&list.size()==0){
			return ToleoResult.build(400, "用户或者密码不正确");
		}
		TbUser user = list.get(0);
		//进行md5 后校验
		if(!DigestUtils.md5DigestAsHex(password.getBytes()).equals(user.getPassword())){
			return ToleoResult.build(400, "用户或者密码不正确");
		}
		String token = UUID.randomUUID().toString();
		user.setPassword(null);
		jedisClient.set(USER_SESSION+":"+token, JsonUtils.objectToJson(user));
		jedisClient.expire(USER_SESSION+":"+token, SESSION_EXPIRE);
		//主干
		
		
		//结果
		return ToleoResult.ok(token);
	}
	//------------------------------------------------------------------------------------------------
	@Override
	public ToleoResult getUserByToken(String token) {
		//主干基础
		String json = jedisClient.get(USER_SESSION+":"+token);
		if(StringUtils.isBlank(json)){
			return ToleoResult.build(400, "用户登录过期");
		}
		jedisClient.expire(USER_SESSION+":"+token, SESSION_EXPIRE);
		TbUser user = JsonUtils.jsonToPojo(json, TbUser.class);
		//主干
		
		//结果
		return ToleoResult.ok(user);
	}
		
	
}
